package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressGetListResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressGetRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressGetResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressRemoveRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressRemoveResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressSaveRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberAddressSaveResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;





/**
 *
 * @author wliu
 *    edi  tse
 */
public class MemberAddressServiceImpl implements MemberAddressService {
	private Context context;
	private static Gson gson = new Gson();


	public MemberAddressServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取用户某个收货地址信息
	 * @param request
	 * @param listener
	 * @return MemberAddressGetResponse
	 */

	    public void get(MemberAddressGetRequest request, final RemoteServiceListenerT< MemberAddressGetResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("consigneeId",request.getConsigneeId());

        final String url = "member/address/get";


            new HttpService().get(context, url, params, MemberAddressGetResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberAddressGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberAddressGetResponse() : (MemberAddressGetResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberAddressGetResponse() : (MemberAddressGetResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 获取当前用户的收货地址列表
	 * @param request
	 * @param listener
	 * @return MemberAddressGetListResponse
	 */

	    public void getList(MemberAddressGetListRequest request, final RemoteServiceListenerT< MemberAddressGetListResponse> listener) {

        final HttpParams params = new HttpParams();



        final String url = "member/address/getList";



        /**
        address
        getList
        GetList
        获取当前用户的收货地址列表
        GET
        MemberAddress
        MemberAddressGetList
        member/address/getList
        array
        */

           final Type type = new TypeToken<List<MemberAddressGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberAddressGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberAddressGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberAddressGetListResponse responce = new MemberAddressGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberAddressGetListResponse());
                 } else {
                     List<MemberAddressGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberAddressGetListResponse responce = new MemberAddressGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberAddressGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberAddressGetListResponse(), fromWhere);
                 } else {
                     List<MemberAddressGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberAddressGetListResponse responce = new MemberAddressGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberAddressGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }

             @Override
             public void onMessage(String message) {
                 super.onMessage(message);
                 listener.onMessage(message);
             }


         });
    }






	/**
	 * 删除用户某地址记录
	 * @param request
	 * @param listener
	 * @return MemberAddressRemoveResponse
	 */

	    public void remove(MemberAddressRemoveRequest request, final RemoteServiceListenerT< MemberAddressRemoveResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("consigneeId",request.getConsigneeId());

        final String url = "member/address/remove";


            new HttpService().post(context, url, params, MemberAddressRemoveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberAddressRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberAddressRemoveResponse() : (MemberAddressRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberAddressRemoveResponse() : (MemberAddressRemoveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




	/**
	 * 保存收货地址
	 * @param request
	 * @param listener
	 * @return MemberAddressSaveResponse
	 */

	    public void save(MemberAddressSaveRequest request, final RemoteServiceListenerT< MemberAddressSaveResponse> listener) {

        final HttpParams params = new HttpParams();


            params.put("consigneeId",request.getConsigneeId());
            params.put("consigneeName",request.getConsigneeName());
            params.put("consigneeTel",request.getConsigneeTel());
            params.put("consigneeCard",request.getConsigneeCard());
            params.put("province",request.getProvince());
            params.put("city",request.getCity());
            params.put("district",request.getDistrict());
            params.put("address",request.getAddress());
            params.put("isDefault",request.getIsDefault());

        final String url = "member/address/save";


            new HttpService().post(context, url, params, MemberAddressSaveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberAddressSaveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberAddressSaveResponse() : (MemberAddressSaveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberAddressSaveResponse() : (MemberAddressSaveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }

            @Override
            public void onMessage(String message) {
                super.onMessage(message);
                listener.onMessage(message);
            }
        });
    }




}